#include <iostream>
#include <string>
#include <cstring>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SIZE 1024
int main(int argc, char* argv[])
{
    if (argc != 3) return 1;
    std::string ip =argv[1];
    uint16_t port = std::stoi(argv[2]);
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);

    struct sockaddr_in svraddr;
    memset(&svraddr, 0, sizeof(svraddr));
    socklen_t addrlen = sizeof(svraddr);
    svraddr.sin_family = AF_INET;
    svraddr.sin_port = htons(port);
    inet_aton(ip.c_str(), &svraddr.sin_addr);

    std::string word;
    char buff[SIZE];

    while (std::getline(std::cin, word))
    {
        sendto(sockfd, word.c_str(), word.size(), 0, (struct sockaddr*)&svraddr, sizeof(svraddr));
        std::cout << word << std::endl;
        int n = recvfrom(sockfd, buff, sizeof(buff), 0, (struct sockaddr*)&svraddr, &addrlen);
        if (n > 0)
        {
            buff[n] = 0;
            std::cout << word <<" : " << buff << std::endl;
        }
    }
    close(sockfd);
    return 0;
}
